package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.entity.User;
import com.pjnkj.thunder.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjnkj
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/thunder/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 修改用户抽奖情况
     *
     * @param user
     */
    @PostMapping("onceday")
    public Body onceday(Integer id) {
        User info = userService.getById(id);
        if (info != null) {
            info.setOnceday(1);
            userService.saveOrUpdate(info);
            return Body.newInstance("抽奖成功");
        }
        return Body.newInstance(Meta.CODE_400, "抽奖失败");
    }

    /**
     * 删除数据
     *
     * @param user
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = userService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
     * 增加数据
     *
     * @param user
     */
    @PostMapping("create")
    public Body create(@Validated(User.class) @RequestBody User user) {
        Boolean flag = userService.saveOrUpdate(user);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
     * 修改数据
     *
     * @param user
     */
    @PostMapping("updatealipay")
    public Body updatealipay(String userid, String alipay) {
        User byId = userService.getById(userid);
        if (byId != null) {
            byId.setAlipay(alipay);
            Boolean flag = userService.updateById(byId);
            if (flag) {
                return Body.newInstance("修改成功");
            } else {
                return Body.newInstance(Meta.CODE_400, "修改失败");
            }
        } else {
            return Body.newInstance(Meta.CODE_400, "没有用户");
        }
    }

    /**
     * 修改支付宝
     *
     * @param user
     */
    @PostMapping("update")
    public Body update(@Validated(User.class) @RequestBody User user) {
        Boolean flag = userService.updateById(user);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
     * 查询数据
     *
     * @param
     */
    @PostMapping("find")
    public Body find(Integer id) {
        User info = userService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 查询数据list
     *
     * @param
     */
    @PostMapping("findlist")
    public Body findlist() {
        List<User> list = userService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 分页查询数据
     *
     * @param
     */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<User> pg = new Page<User>(current, size);
        IPage<User> list = userService.page(pg);
        if (list != null && list.getSize() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


}
